<template>
  <view class="search-drugs">
    <view class="fixed">
      <search-view placeholder="请输入药品名称进行搜索" focusBol @confirm="search" @input="changeSearch" />
    </view>
    <view class="zhanwei"></view>
    <view class="desc">
      <view v-for="item in drugList" :key="item.baseDrugId" class="info" @click="selectDrug({ data: item })">
        <image
          v-if="item.outerPackagePicUrl"
          class="img"
          lazy-load
          mode="aspectFit"
          :src="item.outerPackagePicUrl"
        ></image>
        <image v-else class="img" lazy-load mode="aspectFit" :src="zwtp"></image>
        <view v-if="item.productName === '' || item.productName === '无'" class="name">
          {{ item.commonName }}
        </view>
        <view v-else class="name">{{ item.commonName }} ({{ item.productName }})</view>
        <view class="spe">
          {{ item.preparationUnit }}*{{ item.packageSize }}{{ item.minimumUnit }}/{{ item.packageUnit }}
        </view>
      </view>
      <div v-if="item.select" class="repeat">已添加</div>
    </view>

    <view v-if="!drugList.length" :class="[hasDrugs ? 'ssbd-default' : 'ssbd']"> </view>
  </view>
</template>

<script>
import searchView from '@/components/searchView';
import { getDrugs } from '@/api/drugInfo';
import { hospitalId } from '@/common/baseUrl.js';
import { mapGetters } from 'vuex';
export default {
  components: { searchView },
  data() {
    return {
      zwtp: 'https://wanhuhealth.oss-cn-beijing.aliyuncs.com/wanhuhealth-small-program/wanhu-mini-community/zwtp.png',
      page: 1,
      drugList: [],
      searchVal: '',
      end: true, // 是否已结束
      hasDrugs: true
    };
  },

  onReachBottom() {
    this.onReachPage();
  },
  computed: {
    ...mapGetters(['assessMedicine'])
  },

  onLoad(opt) {},

  methods: {
    changeSearch(val) {
      // console.log('val:', val);
      this.searchVal = val;
      this.hasDrugs = true;
    },
    async search(val) {
      // if (!val) {
      //   uni.showToast({
      //     title: '请输入搜索关键词',
      //     icon: 'none'
      //   });
      //   return;
      // }

      uni.showLoading({
        title: '加载中'
      });
      // this.searchVal = val;
      this.searchVal = val;
      this.page = 1;

      const data = {
        patientId: this.$store.state.join.storeFormData.patientId,
        search: this.searchVal,
        hospitalId: hospitalId,
        skip: (this.page++ - 1) * 10,
        limit: 10
      };
      const res = await getDrugs(data);
      this.drugList = res.data;
      if (res.data.length < 10) {
        this.end = true;
      } else {
        this.end = false;
      }
      console.log('this.drugList:', this.drugList);
      if (this.drugList.length === 0) {
        this.hasDrugs = false;
      }
      uni.hideLoading();
    },

    // 上拉加载
    async onReachPage() {
      if (this.end) return;
      uni.showLoading({
        title: '加载中'
      });
      const data = {
        patientId: this.$store.state.join.storeFormData.patientId,
        search: this.searchVal,
        hospitalId: hospitalId,
        skip: (this.page++ - 1) * 10,
        limit: 10
      };
      const res = await getDrugs(data);
      this.drugList = this.drugList.concat(res.data);

      if (res.data.length < 10) {
        this.end = true;
      }
      uni.hideLoading();
    },

    // 选择药品
    async selectDrug({ data }) {
      console.log('selectDrug data:', data);
      let hasIn = this.assessMedicine.find((item) => {
        return item.baseDrugId === data.baseDrugId;
      });
      if (hasIn) {
        uni.showLoading({
          title: '该药品已添加过了'
        });
      } else {
        this.assessMedicine.push(data);
        this.$store.commit('assessQuestion/SET_ASSESSMEDICINE', this.assessMedicine);
        this.$wxPromise.navigateBack({
          delta: 1
        });
      }
      // uni.$emit('addData', data);

      // data.frequency = data.frequency ? data.frequency : 1;
      // data.useAmount = 1; // 每次吃几片（默认）
      // this.$store.commit('assess/SET_DRUGS', data);
      // this.$store.commit('assess/DEL_SCAN_DRUGS');
      // // eslint-disable-next-line no-undef
      // const routers = getCurrentPages();
      // for (let r = 0; r < routers.length; r++) {
      //   if (routers[r].route === 'pages/diseaseAssess/index') {
      //     uni.navigateBack({
      //       delta: routers.length - 1 - r
      //     });
      //     return;
      //   }
      // }
      // uni.redirectTo({
      //   url: 'pages/diseaseAssess/index'
      // });
    }
  }
};
</script>

<style lang="scss">
.search-drugs {
  padding-bottom: constant(safe-area-inset-bottom);
  padding-bottom: env(safe-area-inset-bottom);
  .fixed {
    position: fixed;
    width: 100vw;
    background: #f4f4f4;
    z-index: 99;
    box-shadow: rgba(0, 0, 0, 0.04) 0px 6rpx 10rpx;
  }
  .zhanwei {
    height: 126rpx;
  }
  .desc {
    margin-left: 24rpx;
    .info {
      background: #ffffff;
      padding-bottom: 24rpx;
      border-radius: 8rpx;
      height: 500rpx;
      width: 340rpx;
      margin-bottom: 22rpx;
      float: left;
      position: relative;
      box-shadow: rgba(149, 157, 165, 0.2) 0px 8rpx 24rpx;
    }
    .info:nth-child(odd) {
      margin-right: 20rpx;
    }

    .img {
      width: 100%;
      height: 340rpx;
      border-radius: 8rpx;
    }
    .name {
      font-size: 28rpx;
      color: rgba(0, 0, 0, 0.9);
      padding: 0 16rpx;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
    }
    .spe {
      margin-top: 24rpx;
      font-size: 24rpx;
      color: rgba(0, 0, 0, 0.3);
      padding: 0 16rpx;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 1;
      -webkit-box-orient: vertical;
    }
  }
  .desc:after {
    content: ' ';
    height: 0;
    display: block;
    clear: both;
  }
  .none-list {
    text-align: center;
    color: #a2a1a1;
    margin-top: 40rpx;
  }
  .repeat {
    width: 96rpx;
    height: 48rpx;
    background: linear-gradient(236deg, #f34e1c 0%, #ef3931 100%);
    border-radius: 8rpx 0px 8rpx 0px;
    color: #ffffff;
    font-size: 24rpx;
    position: absolute;
    top: 0;
    left: 0;
    text-align: center;
    line-height: 48rpx;
  }
}
.ssbd-wrap {
  .ssbd-con {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    font-size: 36rpx;
    text-align: center;
    color: #999999;
  }
}
</style>
